package com.avalon.erp.sys.addon.base.controller;

import com.avalon.core.condition.Condition;
import com.avalon.core.context.Context;
import com.avalon.core.exception.AvalonException;
import com.avalon.core.exception.FieldCheckException;
import com.avalon.core.model.PageInfo;
import com.avalon.core.model.RecordRow;
import com.avalon.core.service.AbstractService;
import com.avalon.core.util.ObjectUtils;
import com.avalon.core.util.RecordRowUtils;
import com.avalon.core.util.StringUtils;
import com.avalon.erp.sys.addon.base.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/service")
public class ServiceV2Controller {

    @Autowired
    private Context context;

    @PostMapping("{serviceName}/add")
    public Object addModel(@PathVariable("serviceName") String serviceName,
                           @RequestBody ServiceModelParam param) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        RecordRow recordRow = RecordRowUtils.convert(serviceBean, param.getValue());

        return serviceBean.insert(recordRow);
    }

    @PostMapping("{serviceName}/update")
    public Integer updateModel(@PathVariable("serviceName") String serviceName,
                               @RequestBody ServiceModelParam param) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        RecordRow recordRow = RecordRowUtils.convert(serviceBean, param.getValue());

        return serviceBean.update(recordRow);
    }

    @PostMapping("{serviceName}/delete")
    public Integer deleteModel(@PathVariable("serviceName") String serviceName,
                               @RequestBody ServiceModelId param) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        RecordRow recordRow = serviceBean.selectOne(Integer.parseInt(param.getId().toString()));
        if (ObjectUtils.isNull(recordRow)) {
            throw new FieldCheckException("数据不存在,无法删除");
        }
        return serviceBean.delete(recordRow);
    }

    @PostMapping("get/{serviceName}/list")
    public RecordRow getList(@PathVariable("serviceName") String serviceName,
                             @RequestBody ServiceModelList param) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        RecordRow recordRow = new RecordRow();
        if (ObjectUtils.isNotEmpty(param.getCondition())) {
            recordRow.put("data", serviceBean.selectList(param.getFields(), param.getCondition()));
        } else {
            recordRow.put("data", serviceBean.selectList(param.getFields(), param.getRpnCondition()));
        }
        return recordRow;
    }

    @PostMapping("get/detail")
    public RecordRow getDetail(@PathVariable("serviceName") String serviceName,
                               @RequestBody ServiceModelField param) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        RecordRow selectOne = null;
        if (StringUtils.isEmpty(param.getFields())) {
            selectOne = serviceBean.selectOne(Condition.equalCondition(serviceBean.getPrimaryKeyName(),
                    param.getId()));
        } else {
            selectOne = serviceBean.selectOne(param.getFields(), param.getId());
        }

        return selectOne;
    }

    @PostMapping("/get/{serviceName}/page/condition")
    public PageInfo getPage(@PathVariable("serviceName") String serviceName,
                            @RequestBody ServiceConditionPage serviceConditionPage) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        if (ObjectUtils.isNotEmpty(serviceConditionPage.getCondition())) {
            return serviceBean.selectPageCondition(serviceConditionPage.getPage(),
                    serviceConditionPage.getFields(),
                    serviceConditionPage.getOrder(),
                    serviceConditionPage.getIsDistinct(),
                    serviceConditionPage.getCondition());
        } else {
            return serviceBean.selectPage(serviceConditionPage.getPage(),
                    serviceConditionPage.getFields(),
                    serviceConditionPage.getOrder(),
                    serviceConditionPage.getIsDistinct(),
                    Condition.parseRPN(serviceConditionPage.getRpnCondition()));
        }
    }

    @PostMapping("/get/{serviceName}/page/condition/high")
    public PageInfo getPage(@PathVariable("serviceName") String serviceName,
                            @RequestBody ServiceConditionHighPage serviceConditionPage) throws AvalonException {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        if (ObjectUtils.isNotEmpty(serviceConditionPage.getCondition())) {
            return serviceBean.selectPageConditionHigh(serviceConditionPage.getPage(),
                    serviceConditionPage.getFields(),
                    serviceConditionPage.getOrder(),
                    serviceConditionPage.getIsDistinct(),
                    serviceConditionPage.getCondition());
        } else {
            return serviceBean.selectPage(serviceConditionPage.getPage(),
                    serviceConditionPage.getFields(),
                    serviceConditionPage.getOrder(),
                    serviceConditionPage.getIsDistinct(),
                    Condition.parseRPN(serviceConditionPage.getRpnCondition()));
        }
    }

    @PostMapping("get/{serviceName}/tree/page/condition")
    public PageInfo getTreePage(@PathVariable("serviceName") String serviceName,
                                @RequestBody ServiceConditionHighPage serviceConditionPage) {
        AbstractService serviceBean = context.getServiceBean(serviceName);
        if (ObjectUtils.isNotEmpty(serviceConditionPage.getCondition())) {
            return serviceBean.selectTreePageCondition(serviceConditionPage.getPage(),
                    serviceConditionPage.getFields(),
                    serviceConditionPage.getOrder(),
                    serviceConditionPage.getCondition());
        } else {
            return serviceBean.selectTreePage(serviceConditionPage.getPage(),
                    serviceConditionPage.getFields(),
                    serviceConditionPage.getOrder(),
                    Condition.parseRPN(serviceConditionPage.getRpnCondition()));
        }
    }
}
